/**
 * Title: UserServiceImp.java
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.service.security.impl;

import com.pm.entity.IndexBean;
import com.pm.entity.po.security.Group;
import com.pm.entity.po.security.Permission;
import com.pm.entity.po.security.User;
import com.pm.mapper.security.GroupMapper;
import com.pm.mapper.security.UserMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.security.UserService;
import com.pm.utils.security.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Title: UserServiceImp
 * 
 * @author wujin
 * @date 2019年12月30日
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

	private UserMapper userMapper;
	@Autowired
	private GroupMapper groupMapper;

	@Resource
	public void setBasemapper(UserMapper userMapper) {
		this.userMapper = userMapper;
		super.setBaseMapper(userMapper);
	}

	/*
	 * (non-Javadoc) <p>Title: loadUserInfo</p> <p>Description: </p>
	 * 
	 * @param user
	 * 
	 * @return
	 * 
	 * @see
	 * com.starhan.star.service.security.UserService#loadUserInfo(com.starhan.star.
	 * bean.test.User)
	 */
	public IndexBean login(User user) {
		User userByData = userMapper.load(user);
		IndexBean bean = new IndexBean();
		if (null != userByData && userByData.getUserId() > 0) {
			String token = "";
			token = JwtTokenUtil.createJwtToken(user.getUsername());
			bean.setUsername(userByData.getUsername());
			bean.setToken(token);
			bean.setDepartmentId(userByData.getDepartmentId());
			bean.setTaskId(userByData.getTaskId());
			Group group = groupMapper.loadGroupById(userByData.getGroupId());
			if (null != group.getPermissions()) {
				List<Permission> result = group.getPermissions();
				List<Permission> newResult = new ArrayList<>();
				List<Permission> firstResult = new ArrayList<>();

				if (null != result) {
					if (null != result) {
						for (Permission p : result) {
							if (null == p.getParentId() || p.getParentId() < 1) {
								newResult.add(p);
							} else {
								firstResult.add(p);
							}
						}
					}
					setPermission(newResult, firstResult);
				}
				group.setPermissions(newResult);
			}
			bean.setGroup(group);
		} else {
			bean = null;
		}
		return bean;
	}

	private void setPermission(List<Permission> newResult, List<Permission> result) {
		List<Permission> overResult = new ArrayList<Permission>();
		List<Permission> secondResult = new ArrayList<Permission>();
		List<Integer> ids = new ArrayList<Integer>();
		List<Integer> ids2 = new ArrayList<Integer>();
		for (Permission p : newResult) {
			for (Permission m : result) {
				if (p.getPermissionId() == m.getParentId() && !ids2.contains(m.getPermissionId())) {
					ids2.add(m.getPermissionId());
					p.getChildrens().add(m);
					secondResult.add(m);
				} else {
					if (!ids.contains(m.getPermissionId())) {
						ids.add(m.getPermissionId());
						overResult.add(m);
					}
				}
			}
		}
		if (overResult.size() > 0) {
			setPermission(secondResult, overResult);
		}
	}

	/*
	 * (non-Javadoc) <p>Title: loadUserInfoByUserName</p> <p>Description: </p>
	 * 
	 * @param userName
	 * 
	 * @return
	 * 
	 * @see com.scs.service.security.UserService#loadUserInfoByUserName(java.lang.
	 * String)
	 */
	@Override
	public User loadUserInfoByUserName(String userName) {
		return userMapper.loadUserInfoByUserName(userName);
	}

	@Override
	public Integer countUserNum(User user) {
		return userMapper.countUserNum(user);
	}

	/*
	 * (non-Javadoc) <p>Title: loadUserInfo</p> <p>Description: </p>
	 * 
	 * @param user
	 * 
	 * @return
	 * 
	 * @see
	 * com.scs.service.security.UserService#loadUserInfo(com.scs.bean.security.User)
	 */
	@Override
	public IndexBean loadUserInfo(User user) {
		IndexBean bean = new IndexBean();
		bean.setUsername(user.getUsername());
		bean.setGroup(groupMapper.loadGroupById(user.getGroupId()));
		return bean;
	}
}
